/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.ilo.viewer;

import com.kungkhies.desktop.library.entities.Ilo;
import java.util.Collection;
import javax.swing.SwingUtilities;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.kungkhies.desktop.ilo.viewer//IloViewer//EN",
autostore = false)
@TopComponent.Description(preferredID = "IloViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.kungkhies.desktop.ilo.viewer.IloViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_IloViewerAction",
preferredID = "IloViewerTopComponent")
public final class IloViewerTopComponent extends TopComponent implements LookupListener
{
    private Ilo ilo;
    private final InstanceContent content;
    private Lookup.Result result = null;

    public IloViewerTopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(IloViewerTopComponent.class, "CTL_IloViewerTopComponent"));
        setToolTipText(NbBundle.getMessage(IloViewerTopComponent.class, "HINT_IloViewerTopComponent"));


        //Create a new instance of our dynamic object:
        content = new InstanceContent();

        //Add the dynamic object to the TopComponent Lookup:
        associateLookup(new AbstractLookup(content));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        packageLabel = new javax.swing.JLabel();
        packageTextField = new javax.swing.JLabel();
        iloLabel = new javax.swing.JLabel();
        iloTextField = new javax.swing.JLabel();
        Notes = new javax.swing.JLabel();
        notesTextField = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(packageLabel, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.packageLabel.text")); // NOI18N

        packageTextField.setForeground(new java.awt.Color(0, 51, 255));
        org.openide.awt.Mnemonics.setLocalizedText(packageTextField, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.packageTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(iloLabel, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.iloLabel.text")); // NOI18N

        iloTextField.setForeground(new java.awt.Color(0, 51, 255));
        org.openide.awt.Mnemonics.setLocalizedText(iloTextField, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.iloTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(Notes, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.Notes.text")); // NOI18N

        notesTextField.setForeground(new java.awt.Color(0, 51, 255));
        org.openide.awt.Mnemonics.setLocalizedText(notesTextField, org.openide.util.NbBundle.getMessage(IloViewerTopComponent.class, "IloViewerTopComponent.notesTextField.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(iloTextField)
                    .addComponent(notesTextField)
                    .addComponent(Notes)
                    .addComponent(iloLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(packageLabel)
                        .addGap(18, 18, 18)
                        .addComponent(packageTextField)))
                .addContainerGap(440, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(packageLabel)
                    .addComponent(packageTextField))
                .addGap(48, 48, 48)
                .addComponent(iloLabel)
                .addGap(18, 18, 18)
                .addComponent(iloTextField)
                .addGap(63, 63, 63)
                .addComponent(Notes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(notesTextField)
                .addContainerGap(323, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Notes;
    private javax.swing.JLabel iloLabel;
    private javax.swing.JLabel iloTextField;
    private javax.swing.JLabel notesTextField;
    private javax.swing.JLabel packageLabel;
    private javax.swing.JLabel packageTextField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened()
    {     
        RequestProcessor.getDefault().post(new Runnable()
        {
            @Override
            public void run()
            {
                readIlo();
            }
        });     
    }

    @Override
    public void componentClosed()
    {
        result.removeLookupListener(this);
        result = null;  
    }

    void writeProperties(java.util.Properties p)
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    private void readIlo()
    {
        //JavaDBSupport.ensureStartedDB();
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                TopComponent tc = WindowManager.getDefault().findTopComponent("ILOExplorerTopComponent");
                if (tc == null)
                {
                    System.out.println("ILO Viewer. readILO: top component in get default is null");
                    return;
                }
                result = tc.getLookup().lookupResult(Ilo.class);
                result.addLookupListener(IloViewerTopComponent.this);
                resultChanged(new LookupEvent(result));
            }
        });
    }

    void readProperties(java.util.Properties p)
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent lookupEvent)
    {
        System.out.println("In result changes IloViewerTopComponent.");
        Lookup.Result r = (Lookup.Result) lookupEvent.getSource();
        Collection<Ilo> coll = (Collection<Ilo>) r.allInstances();
        if (!coll.isEmpty())
        {
            for (Ilo lo : coll)
            {
                ilo = lo;
                packageTextField.setText(lo.getPackage1());
                iloTextField.setText(lo.getIloText());
                
                String notesString = lo.getNote();
                notesString = (notesString == null || notesString.equals("")) ? "--no notes--" : notesString;
                notesTextField.setText(notesString);
            }
        }
        else
        {
            packageTextField.setText("[no selection]");
            iloTextField.setText("");
            notesTextField.setText("");
        }
    }

    public void resetFields()
    {
        packageTextField.setText("");
        iloTextField.setText("");
        notesTextField.setText("");
    }
}
